<?php


function contains_html($pString){
    return (preg_match("/([\<])([^\>]{1,})*([\>])/i", $pString));
}

function truncate($pString, $pMaxLength, $pTrailing='...'){
  $pMaxLength -= mb_strlen($pTrailing);
  if (mb_strlen($pString) > $pMaxLength)
    return mb_substr($pString, 0, $pMaxLength) . $pTrailing;
  else
    return $pString;
}

function get_client_ipaddress(){
  if (!empty($_SERVER['HTTP_CLIENT_IP']))     //check ip from share internet
    $ip=$_SERVER['HTTP_CLIENT_IP'];
  elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))     //to check ip is pass from proxy
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
  else
    $ip=$_SERVER['REMOTE_ADDR'];

  logdebug('get_client_ipaddress', "Recording client ip address - $ip");

  return $ip;
}

function array_get_or($pKey, $pArray, $pOrThis){
  if (array_key_exists($pKey, $pArray))
    return $pArray[$pKey];
  else
    return $pOrThis;
}

//Check to see if the values in the first array are present as keys in the second array.
//If $pAll is true, they must all exist or else this function returns false
function array_keys_exist($pArrayOfValues, $pAssocArray, $pAll=false){
  if ($pAll){
    $all_match = true;
    foreach ($pArrayOfValues as $val){
      if (!array_key_exists($val, $pAssocArray))
	$all_match = false;
    }
    return $all_match;
  } else {
    foreach ($pArrayOfValues as $val){
      if (array_key_exists($val, $pAssocArray))
        return true;
    }
    return false;
  }
}

/**
 * Does the reverse of explodeCase() - takes an array as input and combines all the elements back into a string
 * according to CamelCase format. Uppercase characters are used as the separator between the various segments
 * so all other characters will be taken to lowercase by default (see the preserve parameter). Therefore this
 * will not normally preserve the case of the original strings in the array.
 * @author Charl van Niekerk <charlvn@charlvn.za.net>
 * @param array $array The original array to be converted
 * @param bool $first Should the first character also be taken to uppercase? (e.g. camelCase versus CamelCase)
 * @param bool $preserve Preserve the uppercase characters in the original array and don't convert them.
 * @return string The resulting string combining all of the array elements into CamelCase format.
 */
function implodeCase($array, $first = false, $preserve = false)
{
  // Initialise the string to be returned
  $string = '';

  // Loop through each element in the array
  foreach ($array as $i => $segment) {
    // If the preserve case option has not been set
    if (!$preserve) {
      // Make sure the current segment does not contain uppercase characters
      $segment = strtolower($segment);
    }

    // If it isn't the first segment or the resulting string must start with an uppercase character
    if ($i || $first) {
      // Capitalise the first character of the segment
      $segment = ucfirst($segment);
    }

    // Add this segment to the end of the string
    $string .= $segment;
  }

  // Return the resulting string
  return $string;
}

/**
   * Translates a camel case string into a string with underscores (e.g. firstName -&gt; first_name)
   * @param    string   $str    String in camel case format
   * @return    string            $str Translated into underscore format
   */
  function from_camel_case($str) {
    $str[0] = strtolower($str[0]);
    $func = create_function('$c', 'return "_" . strtolower($c[1]);');
    return preg_replace_callback('/([A-Z])/', $func, $str);
  }

  /**
   * Translates a string with underscores into camel case (e.g. first_name -&gt; firstName)
   * @param    string   $str                     String in underscore format
   * @param    bool     $capitalise_first_char   If true, capitalise the first char in $str
   * @return   string                              $str translated into camel caps
   */
  function to_camel_case($str, $capitalise_first_char = false) {
    if($capitalise_first_char) {
      $str[0] = strtoupper($str[0]);
    }
    $func = create_function('$c', 'return strtoupper($c[1]);');
    return preg_replace_callback('/_([a-z])/', $func, $str);
  }

    function to_url_friendly($pString){
	return preg_replace('/[^a-z0-9-]/', '-', strtolower($pString));
    }

?>